/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pe.edu.pucp.TrabajoGrupal.gui;

import java.awt.Graphics;
import java.sql.*;
import javax.swing.JLabel;

/**
 *
 * @author ronald
 */
public class AdvancesProjectDialog extends javax.swing.JDialog {
    private Connection con = null;
    private Statement st = null;
    private Statement st1 = null;
    private ResultSet rs = null;
    private ResultSet rs2 = null;
    /**
     * Creates new form AdvancesProjectDialog
     */
    public AdvancesProjectDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();   
       
       
    }
    public AdvancesProjectDialog(java.awt.Frame parent, boolean modal,String projectName, String userName) {
        super(parent, modal);
        initComponents();
        this.setBounds(100, 100, 700, 700);
        String user = userName;
        String Project = projectName;        
        int x = 10;
        int y = 10;        
        conectar();
        try{            
            String query2 = "SELECT usuarioid FROM usuario WHERE usuario = '"+ user+ "'";
            st = con.createStatement();
            rs = st.executeQuery(query2);
            while (rs.next()){
                user = rs.getString("usuarioid");
            }
            y+=20;
            addLabels("Projecto = "+ Project +"",x,y);
            //Imprimir: Projecto = Project
            String query3 = "SELECT proyectoid FROM proyecto WHERE proyecto = '"+ Project+ "'";
            st = con.createStatement();
            rs = st.executeQuery(query3);
            while (rs.next()){
                Project = rs.getString("proyectoid");
            }
            String query4 = "SELECT tareaid, tareanombre, horastrabajo FROM tarea WHERE proyectoid = "+ Project+ "";
            st = con.createStatement();
            rs = st.executeQuery(query4);
            int totaladvance=0;
            int offsetx1 = 200;
            while (rs.next()){
                int partialadvance=0;
                int partialpercent=0;
                String taskName = rs.getString("tareanombre");
                int taskTime = rs.getInt("horastrabajo");
                y+=20*2;
               
                addLabels("Tarea = "+ taskName, x, y);
                addLabels("Horas Estimadas ="+ taskTime+"",x+offsetx1,y);
                //Imprimir: Tarea = taskname        Horas Estimadas = tasktime
                String taskId = rs.getString("tareaid");
                String query5 ="SELECT observaciones, horas FROM avance WHERE tareaid = '"+ taskId+ "'" ;                
                y+=20;
                addLabels("Observacion", x, y);
                addLabels("Horas Trabajadas", x+offsetx1, y);
                addLabels("Porcentaje de Horas",x+offsetx1*2,y);
                //Imprimir: Observacion     Horas Trabajadas        Porcentaje de Horas
                st = con.createStatement();
                rs2 = st.executeQuery(query5);
                
                while (rs2.next()){
                    String description = rs2.getString("observaciones");
                    int time = rs2.getInt("horas");
                    int percent = time*100/taskTime;
                    //Imprimir: descripcion     time        percent "%"
                    y+=20;
                    addLabels(""+description, x, y);
                    addLabels(""+ time, x+offsetx1, y);
                    addLabels(""+ percent+ "% ",x+offsetx1*2,y);
                    partialpercent+=percent;                    
                    partialadvance+=time;
                }
                //Imprimir: Total avanzado      partialadvance      partialpercent "%"
                y+=20;
                addLabels("Total Avanzado", x, y);
                addLabels(""+partialadvance, x+offsetx1, y);
                addLabels(""+partialpercent+"%",x+offsetx1*2,y);
                totaladvance+=partialadvance;
            }                        
            //Imprimir: Tiempo Invertido  totaladvance "Horas"
            y+=20*2;
            addLabels("Tiempo Invertido", x, y);
            addLabels(""+totaladvance, x+offsetx1, y);
            addLabels("Horas",x+offsetx1*2,y);
        }
        catch(Exception e){
            e.printStackTrace();
        }
        cerrar();
    }
    
    private void addLabels(String Text, int x, int y){
        JLabel label = new JLabel();
        label.setText(Text);
        label.setVisible(true);
        label.setBounds(x, y, 400, 100);
        this.getContentPane().add(label);
    }
    
    private void conectar(){
         try
        {
            String url = "jdbc:mysql://localhost:3306/dbproyectos";
            String usr = "root";
            String password = "";
            //Class.forName("com.mysql.jdbc.Driver");
            //DriverManager.registerDriver(new org.gjt.mm.mysql.Driver());
            con = DriverManager.getConnection (url,usr, password);            
        } catch (SQLException ex) {

        }
    }
    private void cerrar(){
        try{
            st.close();
            con.close();
        }
        catch(Exception e){
        
        }
    }
    public void GenerateAdvance(String Project, String user){
        conectar();
        try{            
            String query2 = "SELECT usuarioid FROM usuario WHERE usuario = '"+ user+ "'";
            st = con.createStatement();
            rs = st.executeQuery(query2);
            while (rs.next()){
                user = rs.getString("usuarioid");
            }
            //Imprimir: Projecto = Project
            String query3 = "SELECT proyectoid FROM proyecto WHERE proyecto = '"+ Project+ "'";
            st = con.createStatement();
            rs = st.executeQuery(query3);
            while (rs.next()){
                Project = rs.getString("proyectoid");
            }
            String query4 = "SELECT tareaid, tareanombre, horastrabajo FROM tarea WHERE proyectoid = '"+ Project+ "'";
            st = con.createStatement();
            rs = st.executeQuery(query3);
            int totaladvance=0;
            while (rs.next()){
                int partialadvance=0;
                int partialpercent=0;
                String taskName = rs.getString("tareanombre");
                int taskTime = rs.getInt("horastrabajo");
                //Imprimir: Tarea = taskname        Horas Estimadas = tasktime
                String taskId = rs.getString("tareaid");
                String query5 ="SELECT observaciones, horas FROM avance WHERE tareaid = '"+ taskId+ "'" ;                
                //Imprimir: Observacion     Horas Trabajadas        Porcentaje de Horas
                st = con.createStatement();
                rs = st.executeQuery(query4);
                
                while (rs.next()){
                    String description = rs.getString("observaciones");
                    int time = rs.getInt("horas");
                    int percent = time*100/taskTime;
                    //Imprimir: descripcion     time        percent "%"
                    partialpercent+=percent;                    
                    partialadvance+=time;
                }
                //Imprimir: Total avanzado      partialadvance      partialpercent "%"
                totaladvance+=partialadvance;
            }                        
            //Imprimir: Tiempo Invertido  totaladvance "Horas"
        }
        catch(Exception e){
            
        }
        cerrar();
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 459, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(AdvancesProjectDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(AdvancesProjectDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(AdvancesProjectDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(AdvancesProjectDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the dialog
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                AdvancesProjectDialog dialog = new AdvancesProjectDialog(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
}
